package cc.imos.communal.entity;

import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * <pre>
 *     分页对象
 * Created by asher on 2017/3/18.
 * </pre>
 */
@NoArgsConstructor
@ApiModel(value = "分页对象")
public class Page<T> implements Serializable {
    @Getter
    @ApiModelProperty(value = "当前页码")
    private int pageNumber = 1;

    @Getter
    @ApiModelProperty(value = "每页数量")
    private int pageSize = 1;

    @Getter
    @ApiModelProperty(value = "总数据数")
    private int totalRow = 0;

    @Getter
    @ApiModelProperty(value = "总页码数")
    private int totalPage = 0;

    @Getter
    @ApiModelProperty(value = "数据集合")
    private List<T> list = Lists.newArrayList();

    /**
     * Constructor.
     * @param list the list of paginate result
     * @param pageNumber the page number
     * @param pageSize the page size
     * @param totalRow the total row of paginate
     */
    public Page(List<T> list, int pageNumber, int pageSize, int totalRow) {
        this.list = list;
        this.pageNumber = pageNumber;
        this.pageSize = pageSize;
        this.totalRow = totalRow;

        if(totalRow > 0) {
            this.totalPage = totalRow / pageSize;
            if(totalRow % pageSize > 0) {
                this.totalPage = this.totalPage + 1;
            }
        } else {
            this.totalPage = 0;
        }
    }
}
